<template>
  <el-table :data="newsList" stripe style="width: 100%">
    <el-table-column prop="ntitle" label="标题" width="180" #default="scope">
      <el-link @click="toDetail(scope.row.nid)">{{ scope.row.ntitle }}</el-link>
    </el-table-column>
    <el-table-column prop="nsubhead" label="副标题" width="250"/>
    <el-table-column prop="createdate" label="发布时间"/>
    <el-table-column prop="nstatus" label="状态" #default="scope">
      {{
        scope.row.nstatus === 1 ? '等待审核' : scope.row.nstatus === 2 ? '正在显示' : scope.row.nstatus === 3 ? '已隐藏' : '其他'
      }}
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="120">
      <template #default="scope">
        <el-button @click="newHidden(scope.row.nid)" v-if="scope.row.nstatus===2" type="primary" size="small"
                   round>隐藏
        </el-button>
        <el-button @click="newShow(scope.row.nid)" v-if="scope.row.nstatus===3" type="success" size="small"
                   round>显示
        </el-button>
      </template>
    </el-table-column>
  </el-table>
<!--  分页-->
  <div class="pageBox">
    <el-pagination
        :page-size="5"
        small
        background
        layout="prev, pager, next"
        :total="total"
        class="mt-4"
        v-model:current-page="nowPage"
        @update:current-page="getData"
    />
  </div>
</template>

<script>
import doThis from "@/api/getData";
import {ElMessage} from "element-plus";

export default {
  name: "UserNews",
  data() {
    return {
      newsList: [],
      nowPage: 1,
      total: 0,
    }
  },
  methods: {
    newShow(id) {
      console.log(id)
      doThis.doGet("/news/change", {id: id, status: 2}).then(res => {
/*        alert(res.msg)
        if (res.code === "0") {
          this.getData()
        }*/
        if (res.code === "0") {
          ElMessage({
            message: res.msg,
            type: 'success',
          })
          this.getData()
        }else {
          ElMessage.error(res.msg)
        }
      })
    },
    newHidden(id) {
      doThis.doGet("/news/change", {id: id, status: 3}).then(res => {
/*        alert(res.msg)
        if (res.code === "0") {
          this.getData()
        }*/
        if (res.code === "0") {
          ElMessage({
            message: res.msg,
            type: 'success',
          })
          this.getData()
        }else {
          ElMessage.error(res.msg)
        }
      })
    },
    getData() {
      doThis.doGet("/news/findByUser", {pageNum: this.nowPage}).then(res => {
        this.newsList = res.data.list
        this.total = res.data.totle
      })
    },
    toDetail(nid){
      this.$router.push("/front/newsDetail/"+nid)
    },
  },
  mounted() {
    this.getData()
  }
}
</script>

<style scoped>
.pageBox {
  margin-top: 30px;
}
</style>